/******************************************************************************************************************************************************************************************
 * @file   : .c
 * @brief  :
 * @author : SHB
 *=========================================================================================================================================================================================
 *                                                   Revision control History
 *=========================================================================================================================================================================================
 * V1.0.0: 2022-01-04 : Initial Version
 *
 *
 *****************************************************************************************************************************************************************************************/

/******************************************************************************************************************************************************************************************
 * Other header files
 *****************************************************************************************************************************************************************************************/
#include "stdint.h"
#include "fm33le0xxa_fl_gpio.h"

#include "SensorGroup.h"
#include "Sensor.h"
#include "SysInit.h"
/******************************************************************************************************************************************************************************************
 * Macro decalaration
 *****************************************************************************************************************************************************************************************/

/******************************************************************************************************************************************************************************************
 * Enum decalaration
 *****************************************************************************************************************************************************************************************/

/******************************************************************************************************************************************************************************************
 * typedef compound type decalaration
 *****************************************************************************************************************************************************************************************/

/******************************************************************************************************************************************************************************************
 * Struct decalaration
 *****************************************************************************************************************************************************************************************/

/******************************************************************************************************************************************************************************************
 * Local variables decalaration
 *****************************************************************************************************************************************************************************************/

/******************************************************************************************************************************************************************************************
 * Global variables definition
 *****************************************************************************************************************************************************************************************/
Sensor tail_detect;
Sensor stop_detect;

/******************************************************************************************************************************************************************************************
 * Static Local Function decalaration property
 *****************************************************************************************************************************************************************************************/
/**
 * @brief Get analog sample raw value.
 *
 * @param me Sensor pointer
 * @return Sensor_DataType
 */
static Sensor_DataType Sensor_GetAnalogSampleRaw(Sensor *me);

// static Sensor_DataType Sensor_GetDigitalSampleRaw(Sensor *me);

/**
 * @brief Calculate algorithm
 *
 * @param me Sensor pointer
 * @return void
 */
static void Sensor_AnalogCalc(Sensor *me);
//static void Sensor_DigitalCalc( Sensor *me );
//static void Sensor_BinNtcCalc(Sensor *me);

/******************************************************************************************************************************************************************************************
 * Function definition
 *****************************************************************************************************************************************************************************************/

void SensorGroup_Start(void)
{
	GetDMAChannelSample(); 
}

/**
 * @brief
 *
 */
void SensorGroup_Init(void)
{
	uint8_t i = 0;
    
    SensorGroup_Start();
        
	Sensor_Ctor(&tail_detect, 0U, IoHwAb_Channel_TAIL, QUEUE_SIZE);
	Sensor_Ctor(&stop_detect, 0U, IoHwAb_Channel_STOP, QUEUE_SIZE);

	tail_detect.Sensor_GetSampleRawPtr = Sensor_GetAnalogSampleRaw;
	stop_detect.Sensor_GetSampleRawPtr = Sensor_GetAnalogSampleRaw;

	tail_detect.Sensor_CalcPtr = Sensor_AnalogCalc;
	stop_detect.Sensor_CalcPtr = Sensor_AnalogCalc;

    do
    {
        if (ADC_IntFinishFlag)
        {
            i++;
            ADC_IntFinishFlag = 0;
            if (!ADC_TimeOutFlag)
            {
                Sensor_Update(&tail_detect);
                Sensor_Update(&stop_detect);
            }

            SensorGroup_Start();
        }
    } while (i < 8);
}

/**
 * @brief
 *
 */
void SensorGroup_Main(void)
{
	if (ADC_IntFinishFlag)
	{
		ADC_IntFinishFlag = 0;
        if (!ADC_TimeOutFlag)
        {
		Sensor_Update(&tail_detect);
		Sensor_Update(&stop_detect);
        }
		SensorGroup_Start();
	}
}

/**
 * @brief Get analog sample raw value.
 *
 * @param me Sensor pointer
 * @return Sensor_DataType
 */
static Sensor_DataType Sensor_GetAnalogSampleRaw(Sensor *me)
{
	return (uint16_t)ADC_Getdata(me->SensorSampleChannel);
}

#if 0 /*mask ---not used */
/**
 * @brief Get digital sample raw value.
 *
 * @param me Sensor pointer
 * @return Sensor_DataType
 */
static Sensor_DataType Sensor_GetDigitalSampleRaw(Sensor *me)
{
	return (uint16_t)GPIO_ReadPinLevel((PORT_ID_t)((me->SensorSampleChannel)>>8), (PORT_GPIONO_t)((me->SensorSampleChannel)&0xFF));
}
#endif

/**
 * @brief Calculate algorithm
 *
 * @param me Sensor pointer
 * @return void
 */
static void Sensor_AnalogCalc(Sensor *me)
{
	uint8_t i = 0U;
	uint8_t len = me->super.Qused;
	uint16_t SampTmp = 0U;

	uint16_t Max = me->super.Qbuf[0];
	uint16_t Min = me->super.Qbuf[0];

	if (len < 3)
	{
		me->PhysicVal = 0;
		return;
	}

	for (i = 0U; i < len; i++)
	{
		if (Max < me->super.Qbuf[i])
		{
			Max = me->super.Qbuf[i];
		}

		if (Min > me->super.Qbuf[i])
		{
			Min = me->super.Qbuf[i];
		}

		SampTmp += me->super.Qbuf[i];
	}
	SampTmp = (SampTmp - Max - Min) / (len - 2);
	/** Get input voltage */

#if 0 
        //如果使用获取的AD值则加上这段代码
	SampTmp = (uint16_t)(((uint32_t)SampTmp * 5000U) / ANALOG_SAMPLE_RESOLUTION);
#endif
        //使用库函数计算电压，获取的SampTmp为端口电压值。
	me->PhysicVal = ANALOG_ADD_VOLTAGE + SampTmp + (((uint32_t)SampTmp * ANALOG_SAMPLE_PULLUP_RES) / ANALOG_SAMPLE_PULLDOWN_RES);

}

#if 0 /*mask ---not used */
/**
 * @brief Calculate algorithm
 *
 * @param me Sensor pointer
 * @return void
 */
static void Sensor_DigitalCalc( Sensor *me )
{
    uint8_t  i        = 0U;
    uint8_t  len      = me->super.Qused;
    uint16_t SampTmp  = 0U;

    for ( i = 0U; i < len; i++ ) {
        SampTmp += me->super.Qbuf[i];
    }
    SampTmp = SampTmp / len;
    
    me->PhysicVal = (SampTmp>0)?1:0;
}
#endif

#if 0 /*mask ---not used */
/**
 * @brief Calculate BinNtcCalc
 *
 * @param me Sensor pointer
 * @return void
 */
static void Sensor_BinNtcCalc(Sensor *me)
{
	uint8_t i = 0U;
	uint8_t len = me->super.Qused;
	uint16_t SampTmp = 0U;

	uint16_t Max = me->super.Qbuf[0];
	uint16_t Min = me->super.Qbuf[0];

	if (len < 3)
	{
		me->PhysicVal = 0;
		return;
	}

	for (i = 0U; i < len; i++)
	{
		if (Max < me->super.Qbuf[i])
		{
			Max = me->super.Qbuf[i];
		}

		if (Min > me->super.Qbuf[i])
		{
			Min = me->super.Qbuf[i];
		}

		SampTmp += me->super.Qbuf[i];
	}
	SampTmp = (SampTmp - Max - Min) / (len - 2);

	/** Get input voltage */
	SampTmp = (uint16_t)(((uint32_t)SampTmp * 5000U) / ANALOG_SAMPLE_RESOLUTION);
	me->PhysicVal = SampTmp;
}
#endif
/******************************************************************************************************************************************************************************************
 * End of file
 *****************************************************************************************************************************************************************************************/
